package com.yixun.qfbao.hander;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.api.service.AgentRuleService;
import com.yixun.qfbao.apidto.GetAgentRuleResDto;
import com.yixun.qfbao.apidto.ModelListResDto;
import com.yixun.qfbao.apidto.ModelResDto;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.ModelService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GeneralReqVo;
import com.yixun.qfbao.vo.GetModelByNameAndTypeReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	根据经纪人的手机号以及总代companyUuid获取对应的经济公司规则
 */
@Service("getModelByNameHander")
public class GetModelByNameHander extends CommonAbstractHander{

	@Autowired
	ModelService modelService;
	
	@Autowired
	CompanyService companyService;
	
	@Autowired
	LoginUtils loginUtils;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		GetModelByNameAndTypeReqVo reqVo = (GetModelByNameAndTypeReqVo) reqDto;
		
		String userId = reqVo.getParams().getUserId();
		BaseResponseDTO<ModelListResDto> res = new BaseResponseDTO<>();
		PartnerInfo partnerInfo = loginUtils.getUser(userId);
		if(partnerInfo == null) {
			res.setResult(ResultEnum.FAIL.getNo());
			res.setErrorMsg("未登录");
			return res;
		}
		
		String modelName = reqVo.getParams().getModelName();
		String type = reqVo.getParams().getType();
		
		Model model = new Model();
		model.setType(type);
		model.setCompanyId(partnerInfo.getHigherid());
		model.setName(modelName);
		List<Model> modelList = modelService.selectListByDto(model);
		
		if(!CollectionUtils.isEmpty(modelList)) {
			ModelListResDto resData = new ModelListResDto();
			List<ModelResDto> modelResDtoList = ModelResDto.listConvertModelToModelResDto(modelList);
			resData.setModelList(modelResDtoList);
			res.setResData(resData);
		}
		
		res.setResult(ResultEnum.SUCCESS.getNo());
		return res;
		
	}

}
